#include "room_pch.h"
#include "common.h"
#include "BaseApp.h"

#include "BaseWindow.h"

ROOM_BEGIN

LRESULT CALLBACK MainWndProc(
		HWND hwnd,
		UINT msg,
		WPARAM wparam,
		LPARAM lparam
		) {
	BaseWindow * win;
	WindowMessage winmsg(hwnd, msg, wparam, lparam);

	if (msg == WM_CREATE) {
		LPVOID winow_address = reinterpret_cast<LPCREATESTRUCT>(lparam)->lpCreateParams;
		win = reinterpret_cast<BaseWindow *>(winow_address);
		::SetWindowLong(hwnd, 0, reinterpret_cast<LONG>(win));
	}
	else {
		win = reinterpret_cast<BaseWindow *>(::GetWindowLong(hwnd,0));
	}
	return win->process_winow_message(&winmsg);
}

//
// init
//
BaseApp::BaseApp() {
	WNDCLASSEX wcx;
	HINSTANCE hInstance = ::GetModuleHandle(NULL);

	wcx.cbSize = sizeof(wcx);
	wcx.style = CS_HREDRAW | CS_VREDRAW;
	wcx.cbClsExtra = 0;
	wcx.cbWndExtra = sizeof(void*);
	wcx.hInstance = hInstance;
	wcx.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
	wcx.hCursor = ::LoadCursor(NULL, IDC_ARROW);
	wcx.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
	wcx.hIconSm = NULL;
	wcx.lpszMenuName = NULL;
	wcx.lpszClassName = BaseWindowClass;

	wcx.lpfnWndProc = MainWndProc;
	
	if (!RegisterClassEx(&wcx)) {
		std::cerr<<"register class failed"<<std::endl;
		return ;
	}
}
//
// the big message loop
//
int BaseApp::loop() {
	MSG msg;
	while(::GetMessage(&msg, NULL, 0, 0)) {
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
	return msg.wParam;
}

ROOM_END

